package com.datamation.swadeshi.control;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

import com.datamation.swadeshi.control.data.AreaDS;
import com.datamation.swadeshi.control.data.BankDS;
import com.datamation.swadeshi.control.data.BrandDS;
import com.datamation.swadeshi.control.data.CompanyBranchDS;
import com.datamation.swadeshi.control.data.CompanySettingDS;
import com.datamation.swadeshi.control.data.ControlDS;
import com.datamation.swadeshi.control.data.DealerDS;
import com.datamation.swadeshi.control.data.DebtorDS;
import com.datamation.swadeshi.control.data.DiscdebDS;
import com.datamation.swadeshi.control.data.DiscdetDS;
import com.datamation.swadeshi.control.data.DischedDS;
import com.datamation.swadeshi.control.data.DiscslabDS;
import com.datamation.swadeshi.control.data.DiscvdebDS;
import com.datamation.swadeshi.control.data.DiscvdetDS;
import com.datamation.swadeshi.control.data.DiscvhedDS;
import com.datamation.swadeshi.control.data.ExpenseDS;
import com.datamation.swadeshi.control.data.FDDbNoteDS;
import com.datamation.swadeshi.control.data.FIteDebDetDS;
import com.datamation.swadeshi.control.data.FItenrDetDS;
import com.datamation.swadeshi.control.data.FItenrHedDS;
import com.datamation.swadeshi.control.data.FinvDetL3DS;
import com.datamation.swadeshi.control.data.FinvHedL3DS;
import com.datamation.swadeshi.control.data.FreeDebDS;
import com.datamation.swadeshi.control.data.FreeDetDS;
import com.datamation.swadeshi.control.data.FreeHedDS;
import com.datamation.swadeshi.control.data.FreeItemDS;
import com.datamation.swadeshi.control.data.FreeMslabDS;
import com.datamation.swadeshi.control.data.FreeSlabDS;
import com.datamation.swadeshi.control.data.GroupDS;
import com.datamation.swadeshi.control.data.ItemLocDS;
import com.datamation.swadeshi.control.data.ItemPriDS;
import com.datamation.swadeshi.control.data.ItemsDS;
import com.datamation.swadeshi.control.data.LocationsDS;
import com.datamation.swadeshi.control.data.MerchDS;
import com.datamation.swadeshi.control.data.ReasonDS;
import com.datamation.swadeshi.control.data.RouteDS;
import com.datamation.swadeshi.control.data.RouteDetDS;
import com.datamation.swadeshi.control.data.SalRepDS;
import com.datamation.swadeshi.control.data.SkuDS;
import com.datamation.swadeshi.control.data.SubBrandDS;
import com.datamation.swadeshi.control.data.TownDS;
import com.datamation.swadeshi.control.data.TypeDS;
import com.datamation.swadeshi.model.Area;
import com.datamation.swadeshi.model.Bank;
import com.datamation.swadeshi.model.Brand;
import com.datamation.swadeshi.model.CompanyBranch;
import com.datamation.swadeshi.model.CompanySetting;
import com.datamation.swadeshi.model.Control;
import com.datamation.swadeshi.model.Dealer;
import com.datamation.swadeshi.model.Debtor;
import com.datamation.swadeshi.model.Discdeb;
import com.datamation.swadeshi.model.Discdet;
import com.datamation.swadeshi.model.Disched;
import com.datamation.swadeshi.model.Discslab;
import com.datamation.swadeshi.model.Discvdeb;
import com.datamation.swadeshi.model.Discvdet;
import com.datamation.swadeshi.model.Discvhed;
import com.datamation.swadeshi.model.Expense;
import com.datamation.swadeshi.model.FDDbNote;
import com.datamation.swadeshi.model.FIteDebDet;
import com.datamation.swadeshi.model.FItenrDet;
import com.datamation.swadeshi.model.FItenrHed;
import com.datamation.swadeshi.model.FinvDetL3;
import com.datamation.swadeshi.model.FinvHedL3;
import com.datamation.swadeshi.model.FreeDeb;
import com.datamation.swadeshi.model.FreeDet;
import com.datamation.swadeshi.model.FreeHed;
import com.datamation.swadeshi.model.FreeItem;
import com.datamation.swadeshi.model.FreeMslab;
import com.datamation.swadeshi.model.FreeSlab;
import com.datamation.swadeshi.model.Group;
import com.datamation.swadeshi.model.ItemLoc;
import com.datamation.swadeshi.model.ItemPri;
import com.datamation.swadeshi.model.Items;
import com.datamation.swadeshi.model.Locations;
import com.datamation.swadeshi.model.Merch;
import com.datamation.swadeshi.model.Reason;
import com.datamation.swadeshi.model.Route;
import com.datamation.swadeshi.model.RouteDet;
import com.datamation.swadeshi.model.SKU;
import com.datamation.swadeshi.model.SalRep;
import com.datamation.swadeshi.model.SubBrand;
import com.datamation.swadeshi.model.Town;
import com.datamation.swadeshi.model.Type;

public class Downloader extends AsyncTask<Void, Integer, String> {

	Context context;
	DownloadTaskListener taskListener;
	String ConnectionURL, downLoadURL;
	TaskType taskType;
	String downloadingDataType = "";
	String TAG = "Swadeshi";
	int totalRecords=0;
	ProgressDialog progressDialog;

	public Downloader(Context context, DownloadTaskListener taskListener, TaskType taskType, String ConnURL,String downLoadURL) {
		
		this.context = context;
		this.taskListener = taskListener;
		this.ConnectionURL = ConnURL;
		this.taskType = taskType;
		this.downLoadURL=downLoadURL;
		
		this.totalRecords=0;
		
		switch (taskType) {
		case DATABASENAME:
			downloadingDataType = "Main Server";
			break;
			
		case FITEMLOC:
			downloadingDataType = "FItemLoc";
			break;
			
		case FFREESLAB:
			downloadingDataType = "Ffreeslab";
			break;
			
		case FFREEHED :
			downloadingDataType = "Ffreehed";
			break;
			
		case FFREEDET :
			downloadingDataType = "Ffreedet";
			break;
			
		case FFREEDEB :
			downloadingDataType = "Ffreedeb";
			break;
		
		case FITENRDET :
			downloadingDataType = "FItenrDet";
			break;
			
		case FITENRHED:
			downloadingDataType = "fItenrHed";
			break;
			
		case FITEDEBDET:
			downloadingDataType = "fIteDebDet";
			break;
		
		case FITEMPRI:
			downloadingDataType = "fItemPri";
			break;
			
		case FITEMS:
			downloadingDataType ="fItems";
			break;
			
		case FDEBTOR:
			downloadingDataType ="Fdebtor";
			break;
			
		case FCONTROL:
			downloadingDataType ="fControl";
			break;
			
		case FCOMPANYSETTING:
			downloadingDataType ="fCompanySetting";
			break;
			
		case FAREA:
			downloadingDataType ="fArea";
			break;
			
		case FLOCATIONS:
			downloadingDataType ="fLocations";
			break;
			
		case FCOMPANYBRANCH:
			downloadingDataType ="FCompanyBranch";
			break;
			
		case FSALREP:
			downloadingDataType ="fSalRep";
			break;
			
		case FREASON:
			downloadingDataType="fReason";
			break;
			
		case FROUTE:
			downloadingDataType="fRoute";
			break;
			
		case FBANK:
			downloadingDataType="fBank";
			break;
			
		case FDDBNOTE:
			downloadingDataType="fDdbNote";
			break;
			
		case FEXPENSE:
			downloadingDataType="fExpense";
			break;
	
		case FTOWN:
			downloadingDataType="fTown";
			break;
			
		case FMERCH:
			downloadingDataType="FMerch";
			break;
			
		case FROUTEDET:
			downloadingDataType ="fRouteDet";
			break;
	
		case FTRGCAPUL:
			downloadingDataType="FTrgCapUL";
			break;
		
		case FTYPE:
			downloadingDataType ="fType";
			break;
		
		case FSUBBRAND:
			downloadingDataType="fSubBrand";
			break;
			
		case FGROUP:
			downloadingDataType="fGroup";
			break;
		
		case FSKU:
			downloadingDataType="FSKU";
			break;
			
		case FBRAND:
			downloadingDataType="fbrand";
			break;
			
		case FDEALER:
			downloadingDataType="fDealer";
			break;
		
		case FDISCDEB:
			downloadingDataType="Fdiscdeb";
			break;
		
		case FDISCDET:
			downloadingDataType="Fdiscdet";
			break;
			
		case FDISCSLAB:
			downloadingDataType ="Fdiscslab";
			break;
			
		case FDISCHED:
			downloadingDataType ="FDisched";
			break;
			
		case FFREEITEM:
			downloadingDataType="fFreeItem";
			break;
			
		case FFREEMSLAB:
			downloadingDataType="FfreeMslab";
			break;
			
		case FDISCVHED:
			downloadingDataType="FDiscvhed";
			break;	
		
		case FDISCVDET:
			downloadingDataType="FDiscvdet";
			break;
		case FDISCVDEB:
			downloadingDataType="FDiscvdeb";
			break;
		
		case FINVHEDL3:
			downloadingDataType="FinvHedL3";
			break;
			
		case FINVDETL3:
			downloadingDataType="FinvDetL3";
			break;
			
		default:
			break;
		}
	}

//	@Override
//	protected void onPreExecute() {
//		super.onPreExecute();
//		String titleMsg = "Downloading " + downloadingDataType + " data";
//		progressDialog = ProgressDialog.show(context, "Wait", titleMsg);
//
//	}


	
    protected void onPreExecute() {
        super.onPreExecute();
        progressDialog = new ProgressDialog(context);
        progressDialog.setCancelable(false);
        //progressDialog.setTitle("Downloading...");
        progressDialog.show();
    }

	@Override
	protected String doInBackground(Void... params) {
		String resultStr = "";
		
		int recordCount = 0;
		publishProgress(recordCount);
		
		try {
			
			URL json = new URL(ConnectionURL+downLoadURL);

			URLConnection jc = json.openConnection();
			BufferedReader readerfdblist = new BufferedReader(new InputStreamReader(jc.getInputStream()));

			String line = readerfdblist.readLine();
			resultStr = line;
			JSONObject jsonResponse = new JSONObject(line);

			switch (taskType) {
			case DATABASENAME:
				// your work here
				break;
				
			case FITEMLOC:
			{
				JSONArray jsonArray = jsonResponse.getJSONArray("fItemLocResult");
				ArrayList<ItemLoc> list =new ArrayList<ItemLoc>();
				totalRecords=jsonArray.length();
				
				ItemLocDS ds=new ItemLocDS(context);
				
				Log.v(TAG,"Deleted Count: "+ds.deleteAllItemLoc());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					ItemLoc loc=new ItemLoc();
					
					list.clear();
					
					loc.setFITEMLOC_ITEM_CODE(jObject.getString("ItemCode"));
					loc.setFITEMLOC_LOC_CODE(jObject.getString("LocCode"));
					loc.setFITEMLOC_QOH(jObject.getString("QOH"));
					loc.setFITEMLOC_RECORD_ID(jObject.getString("RecordId"));
					
					
					list.add(loc);
					
					++recordCount;
					publishProgress(recordCount);
					
					
					Log.v("createOrUpdateItemLoc", "Result : "+ds.createOrUpdateItemLoc(list));
				}
				
			
			}
				
			break;
				
			case FFREESLAB:
			{
				JSONArray jsonArray = jsonResponse.getJSONArray("FfreeslabResult");
				ArrayList<FreeSlab> list =new ArrayList<FreeSlab>();
				totalRecords=jsonArray.length();
				
				FreeSlabDS ds=new FreeSlabDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					FreeSlab slab=new FreeSlab();
					
					list.clear();					
					
					slab.setFFREESLAB_FITEM_CODE(jObject.getString("Fitemcode"));
					slab.setFFREESLAB_REFNO(jObject.getString("Refno"));
					slab.setFFREESLAB_QTY_F(jObject.getString("Qtyf"));
					slab.setFFREESLAB_QTY_T(jObject.getString("Qtyt"));
					slab.setFFREESLAB_FREE_QTY(jObject.getString("Freeqty"));
					
	//				slab.setFFREESLAB_ADD_USER(jObject.getString("AddUser"));
	//				slab.setFFREESLAB_ADD_DATE(jObject.getString("AddDate"));
	//				slab.setFFREESLAB_ADD_MACH(jObject.getString("AddMach"));
	//				slab.setFFREESLAB_RECORD_ID(jObject.getString("RecordId"));
	//				slab.setFFREESLAB_TIMESTAP_COLUMN(jObject.getString("timestamp_column"));
	//				slab.setFFREESLAB_SEQ_NO(jObject.getString("seqno"));
	//				
					
					list.add(slab);
					
					++recordCount;
					publishProgress(recordCount);
					
					
					Log.v("createOrUpdate - FreeSlab", "Result : "+ds.createOrUpdateFreeSlab(list));
				}
				
			}
			break;
				
			case FFREEHED:
			{
				JSONArray jsonArray = jsonResponse.getJSONArray("FfreehedResult");
				ArrayList<FreeHed> list =new ArrayList<FreeHed>();
				totalRecords=jsonArray.length();
				Log.v(TAG, "Array Length : FreeHed " + jsonArray.length());
				FreeHedDS ds=new FreeHedDS(context);
				
				Log.v(TAG,"Deleted Count FreeHed: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					FreeHed hed =new FreeHed();
					
					list.clear();
					
					hed.setFFREEHED_REFNO(jObject.getString("Refno"));
					hed.setFFREEHED_TXNDATE(jObject.getString("Txndate"));
					hed.setFFREEHED_DISC_DESC(jObject.getString("DiscDesc"));
					hed.setFFREEHED_PRIORITY(jObject.getString("Priority"));
					hed.setFFREEHED_VDATEF(jObject.getString("Vdatef"));
					hed.setFFREEHED_VDATET(jObject.getString("Vdatet"));
					hed.setFFREEHED_REMARKS(jObject.getString("Remarks"));
					hed.setFFREEHED_ITEM_QTY(jObject.getString("ItemQty"));
					hed.setFFREEHED_FREE_IT_QTY(jObject.getString("FreeItQty"));
					hed.setFFREEHED_FTYPE(jObject.getString("Ftype"));
					
					list.add(hed);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFreeHed(list)>0){
						Log.v("CreateOrUpdateFreeHed", "Result : FreeHed Data Inserted successfully");
					}
					
					
				}
							
			}	
			break;
				
			case FFREEDET :
			{
				JSONArray jsonArray = jsonResponse.getJSONArray("FfreedetResult");
				ArrayList<FreeDet> list =new ArrayList<FreeDet>();
				totalRecords=jsonArray.length();
				Log.v(TAG, "Array Length : FreeDet " + jsonArray.length());
				FreeDetDS ds=new FreeDetDS(context);
				
				Log.v(TAG,"Deleted Count FreeHed: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					FreeDet det =new FreeDet();
					
					list.clear();
					
					det.setFFREEDET_REFNO(jObject.getString("Refno"));
					det.setFFREEDET_ITEM_CODE(jObject.getString("Itemcode"));
					
					
					list.add(det);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFreeDet(list)>0){
						Log.v("createOrUpdateFreeDet", "Result : FreeDet Data Inserted successfully");
					}
					
					
				}
							
			}				
			break;
				
			case FFREEDEB :
			//downloadingDataType = "Ffreedeb";
			{
				JSONArray jsonArray = jsonResponse.getJSONArray("FfreedebResult");
				ArrayList<FreeDeb> list =new ArrayList<FreeDeb>();
				totalRecords=jsonArray.length();
				Log.v(TAG, "Array Length : FreeDeb " + jsonArray.length());
				FreeDebDS ds=new FreeDebDS(context);
				
				Log.v(TAG,"Deleted Count FreeDeb: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					FreeDeb deb =new FreeDeb();
					
					list.clear();
					
					deb.setFFREEDEB_REFNO(jObject.getString("Refno"));
					deb.setFFREEDEB_DEB_CODE(jObject.getString("Debcode"));
//					deb.setFFREEDEB_ADD_USER(jObject.getString("AddUser"));
//					deb.setFFREEDEB_ADD_DATE(jObject.getString("AddDate"));
//					deb.setFFREEDEB_ADD_MACH(jObject.getString("AddMach"));
//					deb.setFFREEDEB_TIMESTAMP_COLUMN(jObject.getString("timestamp_column"));
				
					
					list.add(deb);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFreeDeb(list)>0){
						Log.v("createOrUpdateFreeDeb", "Result : FreeDeb Data Inserted successfully");
					}	
				}		
			}
			break;
			
			case FITENRDET :
			//downloadingDataType = "FItenrDet";
			{
				ArrayList<FItenrDet> list =new ArrayList<FItenrDet>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fItenrDetResult");
				Log.v(TAG, "Array Length FItenrDet DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				
				FItenrDetDS ds =new FItenrDetDS(context);
				
				Log.v(TAG,"deleted :"+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					FItenrDet fItenrDet =new FItenrDet();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					fItenrDet.setFITENRDET_NO_OUTLET(jObject.getString("NoOutlet"));
					fItenrDet.setFITENRDET_NO_SHCUCAL(jObject.getString("NoShcuCal"));
					fItenrDet.setFITENRDET_RD_TARGET(jObject.getString("RDTarget"));
					fItenrDet.setFITENRDET_REF_NO(jObject.getString("RefNo"));
					fItenrDet.setFITENRDET_REMARKS(jObject.getString("Remarks"));
					fItenrDet.setFITENRDET_ROUTE_CODE(jObject.getString("RouteCode"));
					fItenrDet.setFITENRDET_TXN_DATE(jObject.getString("TxnDate"));
														
					list.add(fItenrDet);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFItenrDet(list)>0){
						Log.v("createOrUpdatefItenrDet", "Result : fItenrDet Data Inserted successfully");
					}
				}
			}		
			break;
				
			case FITENRHED:
			//downloadingDataType = "fItenrHed";
			{
				ArrayList<FItenrHed> list =new ArrayList<FItenrHed>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fItenrHedResult");
				Log.v(TAG, "Array Length FItenrHed DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				
				FItenrHedDS ds =new FItenrHedDS(context);
				
				Log.v(TAG,"deleted :"+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					FItenrHed fItenrHed =new FItenrHed();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					fItenrHed.setFITENRHED_COST_CODE(jObject.getString("CostCode"));
					fItenrHed.setFITENRHED_DEAL_CODE(jObject.getString("DealCode"));
					fItenrHed.setFITENRHED_MONTH(jObject.getString("Month"));
					fItenrHed.setFITENRHED_REF_NO(jObject.getString("RefNo"));
					fItenrHed.setFITENRHED_REMARKS1(jObject.getString("Remarks1"));
					fItenrHed.setFITENRHED_REP_CODE(jObject.getString("RepCode"));
					fItenrHed.setFITENRHED_YEAR(jObject.getString("Year"));
														
					list.add(fItenrHed);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFItenrHed(list)>0){
						Log.v("createOrUpdatefItenrHed", "Result : fItenrHed Data Inserted successfully");
					}
				}
			}	
			break;
				
			case FITEDEBDET:
			//downloadingDataType = "fIteDebDet";
			{
				ArrayList<FIteDebDet> list =new ArrayList<FIteDebDet>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fIteDebDetResult");
				Log.v(TAG, "Array Length FIteDebDet DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				//String Type="0";
				
				FIteDebDetDS ds =new FIteDebDetDS(context);
				
				Log.v(TAG,"deleted :"+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					FIteDebDet fIteDebDet =new FIteDebDet();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					fIteDebDet.setFITEDEBDET_DEB_CODE(jObject.getString("DebCode"));
					fIteDebDet.setFITEDEBDET_REF_NO(jObject.getString("RefNo"));
					fIteDebDet.setFITEDEBDET_ROUTE_CODE(jObject.getString("RouteCode"));
					fIteDebDet.setFITEDEBDET_TXN_DATE(jObject.getString("TxnDate"));
														
					list.add(fIteDebDet);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFIteDebDet(list)>0){
						Log.v("createOrUpdatefIteDebDet", "Result : fIteDebDet Data Inserted successfully");
					}
				}
			}	
			break;
			
			case FITEMPRI:
			{
				ArrayList<ItemPri> list =new ArrayList<ItemPri>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fItemPriResult");
				Log.v(TAG, "Array Length ItemPri DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				//String Type="0";
				
				ItemPriDS ds =new ItemPriDS(context);
				
				Log.v(TAG,"deleted :"+ds.deleteAllItemPri());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					ItemPri pri =new ItemPri();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					pri.setFITEMPRI_ADD_MACH(jObject.getString("AddMach"));
					pri.setFITEMPRI_ADD_USER(jObject.getString("AddUser"));
					pri.setFITEMPRI_ITEM_CODE(jObject.getString("ItemCode"));
					pri.setFITEMPRI_PRICE(jObject.getString("Price"));
					pri.setFITEMPRI_PRIL_CODE(jObject.getString("PrilCode"));
					pri.setFITEMPRI_SKU_CODE(jObject.getString("SKUCode"));
					pri.setFITEMPRI_TXN_MACH(jObject.getString("TxnMach"));
					pri.setFITEMPRI_TXN_USER(jObject.getString("Txnuser"));
									
					list.add(pri);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateItemPri(list)>0){
						Log.v("createOrUpdateItemPri", "Result : ItemPri Data Inserted successfully");
					}
				}
				

			}	
			break;
				
			case FITEMS:
			//downloadingDataType ="fItems";
			{
				ArrayList<Items> list =new ArrayList<Items>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fItemsResult");
				Log.v(TAG, "Array Length Items DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				
				
				ItemsDS ds =new ItemsDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					Items itm =new Items();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					itm.setFITEM_ADD_MATCH(jObject.getString("AddMach"));
					itm.setFITEM_ADD_USER(jObject.getString("AddUser"));
					itm.setFITEM_AVG_PRICE(jObject.getString("AvgPrice"));
					itm.setFITEM_BRAND_CODE(jObject.getString("BrandCode"));
					itm.setFITEM_GROUP_CODE(jObject.getString("GroupCode"));
					itm.setFITEM_ITEM_CODE(jObject.getString("ItemCode"));
					itm.setFITEM_ITEM_NAME(jObject.getString("ItemName"));
					itm.setFITEM_ITEM_STATUS(jObject.getString("ItemStatus"));
					itm.setFITEM_MAP_CODE(jObject.getString("MapCode"));
					itm.setFITEM_MUST_SALE(jObject.getString("MustSale"));
					itm.setFITEM_NOU_CASE(jObject.getString("NOUCase"));
					itm.setFITEM_ORD_SEQ(jObject.getString("OrdSeq"));
					itm.setFITEM_PRIL_CODE(jObject.getString("PrilCode"));
					itm.setFITEM_RE_ORDER_LVL(jObject.getString("ReOrderLvl"));
					itm.setFITEM_RE_ORDER_QTY(jObject.getString("ReOrderQty"));
					itm.setFITEM_S_BRAND_CODE(jObject.getString("SBrandCode"));
					itm.setFITEM_SKU_CODE(jObject.getString("SKUCode"));
					itm.setFITEM_SKU_SIZ_CODE(jObject.getString("SkuSizCode"));
					itm.setFITEM_TAX_COM_CODE(jObject.getString("TaxComCode"));
					itm.setFITEM_TYPE_CODE(jObject.getString("TypeCode"));
					itm.setFITEM_UNIT_CODE(jObject.getString("UnitCode"));
					itm.setFITEM_VEN_P_CODE(jObject.getString("VenPcode"));
										
					
					list.add(itm);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateItems(list)>0){
						Log.v("createOrUpdateItems", "Result : Items Data Inserted successfully");
					}
					
				}
				

			}	
			break;
			
			case FDEBTOR:
			//downloadingDataType ="Fdebtor";
			{
				ArrayList<Debtor> list =new ArrayList<Debtor>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FdebtorResult");
				Log.v(TAG, "Array Length Debtor DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				
				
				DebtorDS ds =new DebtorDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					Debtor debtor =new Debtor();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					debtor.setFDEBTOR_ADD_MACH(jObject.getString("AddMach"));
					debtor.setFDEBTOR_ADD_USER(jObject.getString("AddUser"));
					debtor.setFDEBTOR_AREA_CODE(jObject.getString("AreaCode"));
					debtor.setFDEBTOR_CHK_CRD_LIMIT(jObject.getString("ChkCrdLmt"));
					debtor.setFDEBTOR_CHK_CRD_PRD(jObject.getString("ChkCrdPrd"));
					debtor.setFDEBTOR_CRD_LIMIT(jObject.getString("CrdLimit"));
					debtor.setFDEBTOR_CRD_PERIOD(jObject.getString("CrdPeriod"));
					debtor.setFDEBTOR_DBGR_CODE(jObject.getString("DbGrCode"));
					debtor.setFDEBTOR_DEAL_CODE(jObject.getString("DealCode"));
					debtor.setFDEBTOR_ADD1(jObject.getString("DebAdd1"));
					debtor.setFDEBTOR_ADD2(jObject.getString("DebAdd2"));
					debtor.setFDEBTOR_ADD3(jObject.getString("DebAdd3"));
					debtor.setFDEBTOR_DEB_CAT_CODE(jObject.getString("DebCatCode"));
					debtor.setFDEBTOR_DEB_CLS_CODE(jObject.getString("DebClsCode"));
					debtor.setFDEBTOR_CODE(jObject.getString("DebCode"));
					debtor.setFDEBTOR_EMAIL(jObject.getString("DebEMail"));
					debtor.setFDEBTOR_LYLTY(jObject.getString("DebLylty"));
					debtor.setFDEBTOR_MOB(jObject.getString("DebMob"));
					debtor.setFDEBTOR_NAME(jObject.getString("DebName"));
					debtor.setFDEBTOR_TELE(jObject.getString("DebTele"));
					debtor.setFDEBTOR_OUT_DIS(jObject.getString("OutDis"));
					debtor.setFDEBTOR_RANK_CODE(jObject.getString("RankCode"));
					debtor.setFDEBTOR_REM_DIS(jObject.getString("RemDis"));
					debtor.setFDEBTOR_REP_CODE(jObject.getString("RepCode"));
					debtor.setFDEBTOR_STATUS(jObject.getString("Status"));
					debtor.setFDEBTOR_TOWN_CODE(jObject.getString("TownCode"));
					debtor.setFDEBTOR_TRAN_BATCH(jObject.getString("TranBatch"));
					
					list.add(debtor);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDebtor(list)>0){
						Log.v("createOrUpdateDebtor", "Result : Debtor Data Inserted successfully");
					}
					
				}
				

			}	
			break;
			
			case FCONTROL:
			{
				ArrayList<Control> list =new ArrayList<Control>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fControlResult");
				Log.v(TAG, "Array Length Control DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				String Type="0";
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					Control ctrl =new Control();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					ctrl.setFCONTROL_COM_ADD1(jObject.getString("ComAdd1"));
					ctrl.setFCONTROL_COM_ADD2(jObject.getString("ComAdd2"));
					ctrl.setFCONTROL_COM_ADD3(jObject.getString("ComAdd3"));
					ctrl.setFCONTROL_COM_NAME(jObject.getString("ComName"));
					ctrl.setFCONTROL_BASECUR(jObject.getString("basecur"));
					ctrl.setFCONTROL_COM_EMAIL(jObject.getString("comemail"));
					ctrl.setFCONTROL_COM_REGNO(jObject.getString("comRegNo"));
					ctrl.setFCONTROL_COM_TEL1(jObject.getString("comtel1"));
					ctrl.setFCONTROL_COM_TEL2(jObject.getString("comtel2"));
					ctrl.setFCONTROL_COM_FAX(jObject.getString("comfax1"));
					ctrl.setFCONTROL_COM_WEB(jObject.getString("comweb"));
					ctrl.setFCONTROL_CONAGE1(jObject.getString("conage1"));
					ctrl.setFCONTROL_CONAGE2(jObject.getString("conage2"));
					ctrl.setFCONTROL_CONAGE3(jObject.getString("conage3"));
					ctrl.setFCONTROL_CONAGE4(jObject.getString("conage4"));
					ctrl.setFCONTROL_CONAGE5(jObject.getString("conage5"));
					ctrl.setFCONTROL_CONAGE6(jObject.getString("conage6"));
					ctrl.setFCONTROL_CONAGE7(jObject.getString("conage7"));
					ctrl.setFCONTROL_CONAGE8(jObject.getString("conage8"));
					ctrl.setFCONTROL_CONAGE9(jObject.getString("conage9"));
					ctrl.setFCONTROL_CONAGE10(jObject.getString("conage10"));
					ctrl.setFCONTROL_CONAGE11(jObject.getString("conage11"));
					ctrl.setFCONTROL_CONAGE12(jObject.getString("conage12"));
					ctrl.setFCONTROL_CONAGE13(jObject.getString("conage13"));
					ctrl.setFCONTROL_CONAGE14(jObject.getString("conage14"));
					ctrl.setFCONTROL_SYSTYPE(jObject.getString("SysType"));
					ctrl.setFCONTROL_SALESACC(jObject.getString("salesacc"));
					
					Type=jObject.getString("SysType");
					
					list.add(ctrl);
					
					++recordCount;
					publishProgress(recordCount);
				}
				
				ControlDS ds =new ControlDS(context);
				if(ds.createOrUpdateFControl(list)>0){
					
					Log.v("createOrUpdateControl", "Result : Control Data Inserted successfully");
					
					return ""+Type;
				}
			}	
			break;
				
			case FCOMPANYSETTING:
			{
				ArrayList<CompanySetting> list =new ArrayList<CompanySetting>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fCompanySettingResult");
				Log.v(TAG, "Array Length CompanySetting DB :" + jsonArray.length());
				totalRecords=jsonArray.length();
				
				
				CompanySettingDS ds =new CompanySettingDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
				
					CompanySetting setting =new CompanySetting();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					setting.setFCOMPANYSETTING_CHAR_VAL(jObject.getString("cCharVal"));
					setting.setFCOMPANYSETTING_COMPANY_CODE(jObject.getString("cCompanyCode"));
					setting.setFCOMPANYSETTING_LOCATION_CHAR(jObject.getString("cLocationChar"));
					setting.setFCOMPANYSETTING_REMARKS(jObject.getString("cRemarks"));
					setting.setFCOMPANYSETTING_GRP(jObject.getString("cSettingGrp"));
					setting.setFCOMPANYSETTING_SETTINGS_CODE(jObject.getString("cSettingsCode"));
					setting.setFCOMPANYSETTING_NUM_VAL(jObject.getString("nNumVal"));
					setting.setFCOMPANYSETTING_TYPE(jObject.getString("nType"));
										
					list.add(setting);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFCompanySetting(list)>0){
						Log.v("createOrUpdateCompanySetting", "Result : CompanySetting Data Inserted successfully");
					}
					
				}
				

			}
				
				break;
				
			case FAREA:
			{
				ArrayList<Area> list =new ArrayList<Area>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fAreaResult");
				Log.v(TAG, "Array Length Area DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				AreaDS ds =new AreaDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Area area =new Area();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					area.setFAREA_ADD_MACH(jObject.getString("AddMach"));
					area.setFAREA_ADD_USER(jObject.getString("AddUser"));
					area.setFAREA_AREA_CODE(jObject.getString("AreaCode"));
					area.setFAREA_AREA_NAME(jObject.getString("AreaName"));
					area.setFAREA_DEAL_CODE(jObject.getString("DealCode"));
					area.setFAREA_REQ_CODE(jObject.getString("RegCode"));
															
					list.add(area);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateArea(list)>0){
						Log.v("createOrUpdateArea", "Result : fArea Data Inserted successfully");
					}
					
				}
				

			}
			break;
			
			case FLOCATIONS:
			{
				ArrayList<Locations> list =new ArrayList<Locations>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fLocationsResult");
				Log.v(TAG, "Array Length fLocations DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				LocationsDS ds =new LocationsDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Locations locations =new Locations();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					locations.setFLOCATIONS_ADD_MACH(jObject.getString("AddMach"));
					locations.setFLOCATIONS_ADD_USER(jObject.getString("AddUser"));
					locations.setFLOCATIONS_LOC_CODE(jObject.getString("LocCode"));
					locations.setFLOCATIONS_LOC_NAME(jObject.getString("LocName"));
					locations.setFLOCATIONS_LOC_T_CODE(jObject.getString("LoctCode"));
					locations.setFLOCATIONS_REP_CODE(jObject.getString("RepCode"));
															
					list.add(locations);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFLocations(list)>0){
						Log.v("createOrUpdateLocations", "Result : Locations Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FCOMPANYBRANCH:
			{
				ArrayList<CompanyBranch> list =new ArrayList<CompanyBranch>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FCompanyBranchResult");
				Log.v(TAG, "Array Length CompanyBranch DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				CompanyBranchDS ds =new CompanyBranchDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					CompanyBranch branch =new CompanyBranch();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					branch.setFCOMPANYBRANCH_BRANCH_CODE(jObject.getString("BranchCode"));
					branch.setFCOMPANYBRANCH_CSETTINGS_CODE(jObject.getString("cSettingsCode"));
					branch.setFCOMPANYBRANCH_NNUM_VAL(jObject.getString("nNumVal"));
					
															
					list.add(branch);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFCompanyBranch(list)>0){
						Log.v("CreateOrUpdateFCompanyBranch", "Result : CompanyBranch Data Inserted successfully");
					}
					
				}
			}	
			break;
				
			case FSALREP:
			{
				ArrayList<SalRep> list =new ArrayList<SalRep>();
				JSONArray jsonArrayfSalRep = jsonResponse.getJSONArray("fSalRepResult");
				Log.v(TAG, "Array Length fSalRep :" + jsonArrayfSalRep.length());
				totalRecords=jsonArrayfSalRep.length();
				for (int i = 0; i < jsonArrayfSalRep.length(); i++) {
					
					SalRep rep =new SalRep();
					
					JSONObject jObject = (JSONObject) jsonArrayfSalRep.get(i);
					
					rep.setFSALREP_ASE_CODE(jObject.getString("ASECode"));
					rep.setFSALREP_AREA_CODE(jObject.getString("AreaCode"));
					rep.setFSALREP_DEAL_CODE(jObject.getString("DealCode"));
					rep.setFSALREP_RECORD_ID(jObject.getString("RecordId"));
					rep.setFSALREP_REP_CODE(jObject.getString("RepCode"));
					rep.setFSALREP_REP_PREFIX(jObject.getString("RepPrefix"));
					rep.setFSALREP_REP_TCODE(jObject.getString("RepTCode"));
					rep.setFSALREP_REP_PHONE_NO(jObject.getString("RepTele"));
					rep.setFSALREP_REP_MOB(jObject.getString("RepMob"));
					rep.setFSALREP_REP_EMAIL(jObject.getString("RepEMail"));
					rep.setFSALREP_REP_NAME(jObject.getString("RepName"));
					rep.setFSALREP_PASSWORD(jObject.getString("Password"));
					
					list.add(rep);
					
					++recordCount;
					publishProgress(recordCount);
				}
				
				SalRepDS ds=new SalRepDS(context);
				if(ds.createOrUpdateSalRep(list)>0){
					
					Log.v("createOrUpdateSalRep", "Result : SalRep Data Inserted successfully");
					return ""+jsonArrayfSalRep.length();
				}
				
			}
				break;
				
			case FREASON:
			{
				ArrayList<Reason> list =new ArrayList<Reason>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fReasonResult");
				Log.v(TAG, "Array Length Reason DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				ReasonDS ds =new ReasonDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Reason reason =new Reason();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					reason.setFREASON_ADD_DATE(jObject.getString("AddDate"));
					reason.setFREASON_ADD_MACH(jObject.getString("AddMach"));
					reason.setFREASON_ADD_USER(jObject.getString("AddUser"));
					reason.setFREASON_CODE(jObject.getString("ReaCode"));
					reason.setFREASON_NAME(jObject.getString("ReaName"));
					reason.setFREASON_REATCODE(jObject.getString("ReaTcode"));
					reason.setFREASON_TYPE(jObject.getString("Type"));
															
					list.add(reason);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFReason(list)>0){
						Log.v("CreateOrUpdateFReason", "Result : FReason Data Inserted successfully");
					}
					
				}
			}	
				break;
				
			case FROUTE:
			
			{
				ArrayList<Route> list =new ArrayList<Route>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fRouteResult");
				Log.v(TAG, "Array Length Route DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				RouteDS ds =new RouteDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Route route =new Route();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					route.setFROUTE_ADDDATE(jObject.getString("AddDate"));
					route.setFROUTE_ADD_MACH(jObject.getString("AddMach"));
					route.setFROUTE_ADD_USER(jObject.getString("AddUser"));
					route.setFROUTE_AREACODE(jObject.getString("AreaCode"));
					route.setFROUTE_DEALCODE(jObject.getString("DealCode"));
					route.setFROUTE_FREQNO(jObject.getString("FreqNo"));
					route.setFROUTE_KM(jObject.getString("Km"));
					route.setFROUTE_MINPROCALL(jObject.getString("MinProcall"));
					route.setFROUTE_RDALORATE(jObject.getString("RDAloRate"));
					route.setFROUTE_RDTARGET(jObject.getString("RDTarget"));
					route.setFROUTE_REMARKS(jObject.getString("Remarks"));
					route.setFROUTE_REPCODE(jObject.getString("RepCode"));
					route.setFROUTE_ROUTECODE(jObject.getString("RouteCode"));
					route.setFROUTE_ROUTE_NAME(jObject.getString("RouteName"));
					route.setFROUTE_STATUS(jObject.getString("Status"));
					route.setFROUTE_TONNAGE(jObject.getString("Tonnage"));
					
					list.add(route);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFRoute(list)>0){
						Log.v("CreateOrUpdateFRoute", "Result : Route Data Inserted successfully");
					}
					
				}
			}	
			break;
				
			case FBANK:
			//downloadingDataType="fBank";
			{
				ArrayList<Bank> list =new ArrayList<Bank>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fbankResult");
				Log.v(TAG, "Array Length Bank DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				BankDS ds =new BankDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Bank bank =new Bank();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					bank.setFBANK_BANK_CODE(jObject.getString("Bankcode"));
					bank.setFBANK_BANK_NAME(jObject.getString("Bankname"));
					bank.setFBANK_BANK_ACC_NO(jObject.getString("Bankaccno"));
					bank.setFBANK_BRANCH(jObject.getString("Branch"));
					bank.setFBANK_ADD1(jObject.getString("Bankadd1"));
					bank.setFBANK_ADD2(jObject.getString("Bankadd2"));
					bank.setFBANK_ADD_DATE(jObject.getString("AddDate"));
					bank.setFBANK_ADD_MACH(jObject.getString("AddMach"));
					bank.setFBANK_ADD_USER(jObject.getString("AddUser"));

					list.add(bank);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateBank(list)>0){
						Log.v("CreateOrUpdateFbank", "Result : bank Data Inserted successfully");
					}
					
				}
			}	
				
			break;
				
			case FDDBNOTE:
			//downloadingDataType="fDdbNote";
			{
				ArrayList<FDDbNote> list =new ArrayList<FDDbNote>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fDdbNoteWithConditionResult");
				Log.v(TAG, "Array Length fDdbNote DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				FDDbNoteDS ds =new FDDbNoteDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					FDDbNote fdDbNote =new FDDbNote();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					fdDbNote.setFDDBNOTE_ADD_DATE(jObject.getString("AddDate"));
					fdDbNote.setFDDBNOTE_ADD_MACH(jObject.getString("AddMach"));
					fdDbNote.setFDDBNOTE_ADD_USER(jObject.getString("AddUser"));
					fdDbNote.setFDDBNOTE_AMT(jObject.getString("Amt"));
					fdDbNote.setFDDBNOTE_B_AMT(jObject.getString("BAmt"));
					fdDbNote.setFDDBNOTE_B_TAX_AMT(jObject.getString("BTaxAmt"));
					fdDbNote.setFDDBNOTE_CUR_CODE(jObject.getString("CurCode"));
					fdDbNote.setFDDBNOTE_CUR_RATE(jObject.getString("CurRate"));
					fdDbNote.setFDDBNOTE_DEB_CODE(jObject.getString("DebCode"));
					fdDbNote.setFDDBNOTE_MANU_REF(jObject.getString("ManuRef"));
					fdDbNote.setFDDBNOTE_OV_PAY_AMT(jObject.getString("OvPayAmt"));
					fdDbNote.setFDDBNOTE_REF_INV(jObject.getString("RefInv"));
					fdDbNote.setFDDBNOTE_REFNO(jObject.getString("RefNo"));
					fdDbNote.setFDDBNOTE_REFNO1(jObject.getString("RefNo1"));
					fdDbNote.setFDDBNOTE_REMARKS(jObject.getString("Remarks"));
					fdDbNote.setFDDBNOTE_REP_CODE(jObject.getString("RepCode"));
					fdDbNote.setFDDBNOTE_SALE_REF_NO(jObject.getString("SaleRefNo"));
					fdDbNote.setFDDBNOTE_TAX_AMT(jObject.getString("TaxAmt"));
					fdDbNote.setFDDBNOTE_TAX_COM_CODE(jObject.getString("TaxComCode"));
					fdDbNote.setFDDBNOTE_TOT_BAL(jObject.getString("TotBal"));
					fdDbNote.setFDDBNOTE_TOT_BAL1(jObject.getString("TotBal1"));
					fdDbNote.setFDDBNOTE_TXN_DATE(jObject.getString("TxnDate"));
					fdDbNote.setFDDBNOTE_TXN_TYPE(jObject.getString("TxnType"));
					
					list.add(fdDbNote);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFDDbNote(list)>0){
						Log.v("CreateOrUpdateFexpense", "Result : expense Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FEXPENSE:
			//downloadingDataType="fExpense";
			{
				ArrayList<Expense> list =new ArrayList<Expense>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fExpenseResult");
				Log.v(TAG, "Array Length Expense DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				ExpenseDS ds =new ExpenseDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Expense expense =new Expense();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					expense.setFEXPENSE_ADD_DATE(jObject.getString("AddDate"));
					expense.setFEXPENSE_ADD_MACH(jObject.getString("AddMach"));
					expense.setFEXPENSE_ADD_USER(jObject.getString("AddUser"));
					expense.setFEXPENSE_CODE(jObject.getString("ExpCode"));
					expense.setFEXPENSE_GRP_CODE(jObject.getString("ExpGrpCode"));
					expense.setFEXPENSE_NAME(jObject.getString("ExpName"));
					expense.setFEXPENSE_STATUS(jObject.getString("Status"));
					
					list.add(expense);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFExpense(list)>0){
						Log.v("CreateOrUpdateFexpense", "Result : expense Data Inserted successfully");
					}
					
				}
			}
			break;
		
			case FTOWN:
			//downloadingDataType="fTown";
			{
				ArrayList<Town> list =new ArrayList<Town>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fTownResult");
				Log.v(TAG, "Array Length Route DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				TownDS ds =new TownDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Town town =new Town();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					town.setFTOWN_ADDDATE(jObject.getString("AddDate"));
					town.setFTOWN_ADD_MACH(jObject.getString("AddMach"));
					town.setFTOWN_ADD_USER(jObject.getString("AddUser"));
					town.setFTOWN_DISTR_CODE(jObject.getString("DistrCode"));
					town.setFTOWN_CODE(jObject.getString("TownCode"));
					town.setFTOWN_NAME(jObject.getString("TownName"));
														
					list.add(town);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateTown(list)>0){
						Log.v("CreateOrUpdateFRoute", "Result : Route Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FMERCH:
			 //downloadingDataType="FMerch";
			{
				ArrayList<Merch> list =new ArrayList<Merch>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FMerchResult");
				Log.v(TAG, "Array Length Merch DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				MerchDS ds =new MerchDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Merch merch =new Merch();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					merch.setFMERCH_CODE(jObject.getString("MerchCode"));
					merch.setFMERCH_NAME(jObject.getString("MerchName"));
					merch.setFMERCH_ADD_USER(jObject.getString("AddUser"));
					merch.setFMERCH_ADD_DATE(jObject.getString("AddDate"));
					merch.setFMERCH_ADD_MACH(jObject.getString("AddMach"));
					merch.setFMERCH_TIMESTAMP_COLUMN(jObject.getString("timestamp_column"));
														
					list.add(merch);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFMerch(list)>0){
						Log.v("CreateOrUpdateFmerch", "Result : merch Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FROUTEDET:
			//downloadingDataType ="fRouteDet";
			{
				ArrayList<RouteDet> list =new ArrayList<RouteDet>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fRouteDetResult");
				Log.v(TAG, "Array Length RouteDet DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				RouteDetDS ds =new RouteDetDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					RouteDet routeDet =new RouteDet();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					routeDet.setFROUTEDET_DEB_CODE(jObject.getString("DebCode"));
					routeDet.setFROUTEDET_ROUTE_CODE(jObject.getString("RouteCode"));
																			
					list.add(routeDet);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFRouteDet(list)>0){
						Log.v("CreateOrUpdatefRouteDet", "Result : Route Data Inserted successfully");
					}
					
				}
			}	
			break;
		
			case FTRGCAPUL:
				downloadingDataType="FTrgCapUL";
				break;
			
			case FTYPE:
			{
				ArrayList<Type> list =new ArrayList<Type>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fTypeResult");
				Log.v(TAG, "Array Length Type DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				TypeDS ds =new TypeDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Type type =new Type();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					type.setFTYPE_ADD_DATE(jObject.getString("AddDate"));
					type.setFTYPE_ADD_MACH(jObject.getString("AddMach"));
					type.setFTYPE_ADD_USER(jObject.getString("AddUser"));
					type.setFTYPE_CODE(jObject.getString("TypeCode"));
					type.setFTYPE_NAME(jObject.getString("TypeName"));
				
														
					list.add(type);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateType(list)>0){
						Log.v("CreateOrUpdateFRoute", "Result : Route Data Inserted successfully");
					}
					
				}
			}
			break;
			
			case FSUBBRAND:
			//downloadingDataType="fSubBrand";
			{
				ArrayList<SubBrand> list =new ArrayList<SubBrand>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fSubBrandResult");
				Log.v(TAG, "Array Length SubBrand DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				SubBrandDS ds =new SubBrandDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					SubBrand subBrand =new SubBrand();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					subBrand.setFSUBBRAND_ADD_DATE(jObject.getString("AddDate"));
					subBrand.setFSUBBRAND_ADD_MACH(jObject.getString("AddMach"));
					subBrand.setFSUBBRAND_ADD_USER(jObject.getString("AddUser"));
					subBrand.setFSUBBRAND_CODE(jObject.getString("SBrandCode"));
					subBrand.setFSUBBRAND_NAME(jObject.getString("SBrandName"));
				
														
					list.add(subBrand);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateSubBrand(list)>0){
						Log.v("CreateOrUpdateFsubBrand", "Result : subBrand Data Inserted successfully");
					}
					
				}
			}				
			break;
				
			case FGROUP:
				//downloadingDataType="fGroup";
			{
				ArrayList<Group> list =new ArrayList<Group>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fGroupResult");
				Log.v(TAG, "Array Length Group DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				GroupDS ds =new GroupDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Group group =new Group();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					group.setFGROUP_ADD_DATE(jObject.getString("AddDate"));
					group.setFGROUP_ADD_MACH(jObject.getString("AddMach"));
					group.setFGROUP_ADD_USER(jObject.getString("AddUser"));
					group.setFGROUP_CODE(jObject.getString("GroupCode"));
					group.setFGROUP_NAME(jObject.getString("GroupName"));
					
					list.add(group);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateGroup(list)>0){
						Log.v("createOrUpdateGroup", "Result : group Data Inserted successfully");
					}
					
				}
			}
			break;
			
			case FSKU:
			//downloadingDataType="FSKU";
			{
				ArrayList<SKU> list =new ArrayList<SKU>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FSKUResult");
				Log.v(TAG, "Array Length SKU DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				SkuDS ds =new SkuDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					SKU sku =new SKU();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					sku.setFSKU_ADD_DATE(jObject.getString("AddDate"));
					sku.setFSKU_ADD_MACH(jObject.getString("AddMach"));
					sku.setFSKU_ADD_USER(jObject.getString("AddUser"));
					sku.setFSKU_BRAND_CODE(jObject.getString("BrandCode"));
					sku.setFSKU_GROUP_CODE(jObject.getString("GroupCode"));
					sku.setFSKU_ITEM_STATUS(jObject.getString("ItemStatus"));
					sku.setFSKU_MUST_SALE(jObject.getString("MustSale"));
					sku.setFSKU_NOUCASE(jObject.getString("NOUCase"));
					sku.setFSKU_ORDSEQ(jObject.getString("OrdSeq"));
					sku.setFSKU_SUB_BRAND_CODE(jObject.getString("SBrandCode"));
					sku.setFSKU_CODE(jObject.getString("SKUCode"));
					sku.setFSKU_NAME(jObject.getString("SkuName"));
					sku.setFSKU_SIZE_CODE(jObject.getString("SkuSizCode"));
					sku.setFSKU_TONNAGE(jObject.getString("Tonnage"));
					sku.setFSKU_TYPE_CODE(jObject.getString("TypeCode"));
					sku.setFSKU_UNIT(jObject.getString("Unit"));
					
					list.add(sku);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateSku(list)>0){
						Log.v("createOrUpdateSku", "Result : sku Data Inserted successfully");
					}
					
				}
			}	
			break;
				
			case FBRAND:
			//downloadingDataType="fbrand";
			{
				ArrayList<Brand> list =new ArrayList<Brand>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FbrandResult");
				Log.v(TAG, "Array Length Brand DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				BrandDS ds =new BrandDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Brand brand =new Brand();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					brand.setFBRAND_ADD_MACH(jObject.getString("AddMach"));
					brand.setFBRAND_ADD_USER(jObject.getString("AddUser"));
					brand.setFBRAND_CODE(jObject.getString("BrandCode"));
					brand.setFBRAND_NAME(jObject.getString("BrandName"));
											
					list.add(brand);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateBrand(list)>0){
						Log.v("createOrUpdateBrand", "Result : brand Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FDEALER:
			{
				ArrayList<Dealer> list =new ArrayList<Dealer>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fDealerResult");
				Log.v(TAG, "Array Length Dealer DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
				
				
				DealerDS ds =new DealerDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Dealer dealer =new Dealer();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					dealer.setFDEALER_ADD_DATE(jObject.getString("AddDate"));
					dealer.setFDEALER_ADD_MACH(jObject.getString("AddMach"));
					dealer.setFDEALER_ADD_USER(jObject.getString("AddUser"));
					dealer.setFDEALER_CUS_TYP_CODE(jObject.getString("CusTypCode"));
					dealer.setFDEALER_DEAL_ADD1(jObject.getString("DealAdd1"));
					dealer.setFDEALER_DEAL_ADD2(jObject.getString("DealAdd2"));
					dealer.setFDEALER_DEAL_ADD3(jObject.getString("DealAdd3"));
					dealer.setFDEALER_DEAL_CODE(jObject.getString("DealCode"));
					dealer.setFDEALER_DEAL_EMAIL(jObject.getString("DealEMail"));
					dealer.setFDEALER_DEAL_GD_CODE(jObject.getString("DealGdCode"));
					dealer.setFDEALER_DEAL_MOB(jObject.getString("DealMob"));
					dealer.setFDEALER_DEAL_NAME(jObject.getString("DealName"));
					dealer.setFDEALER_DEAL_TELE(jObject.getString("DealTele"));
					dealer.setFDEALER_DISTANCE(jObject.getString("Distance"));
					dealer.setFDEALER_STATUS(jObject.getString("Status"));
					dealer.setFDEALER_PREFIX(jObject.getString("DealPreFix"));
					
					list.add(dealer);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFDealer(list)>0){
						Log.v("CreateOrUpdateFDealer", "Result : Dealer Data Inserted successfully");
					}
					
				}
			}	
			break;
			
			case FDISCDEB:
			//downloadingDataType="Fdiscdeb";
			{
				ArrayList<Discdeb> list =new ArrayList<Discdeb>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FdiscdebResult");
				Log.v(TAG, "Array Length discdeb DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscdebDS ds =new DiscdebDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discdeb discdeb =new Discdeb();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discdeb.setFDISCDEB_REF_NO(jObject.getString("RefNo"));
					discdeb.setFDISCDEB_DEB_CODE(jObject.getString("debcode"));
					discdeb.setFDISCDEB_TIEMSTAMP_COLUMN(jObject.getString("timestamp_column"));
					
					list.add(discdeb);
					
					++recordCount;
					publishProgress(recordCount);

					if(ds.createOrUpdateDiscdeb(list)>0){
						Log.v("createOrUpdateDiscdeb", "Result : discdeb Data Inserted successfully");
					}
					
				}
			}		
			break;
			
			case FDISCDET:
			//downloadingDataType="Fdiscdet";
			{
				ArrayList<Discdet> list =new ArrayList<Discdet>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FdiscdetResult");
				Log.v(TAG, "Array Length Disched DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscdetDS ds =new DiscdetDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discdet discdet =new Discdet();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discdet.setFDISCDET_REF_NO(jObject.getString("RefNo"));
					discdet.setFDISCDET_ITEM_CODE(jObject.getString("itemcode"));
					discdet.setFDISCHED_TIEMSTAMP_COLUMN(jObject.getString("timestamp_column"));
					
					list.add(discdet);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDiscdet(list)>0){
						Log.v("createOrUpdateDiscdet", "Result : discdet Data Inserted successfully");
					}
					
				}
			}	
			break;
				
			case FDISCSLAB:
			//downloadingDataType ="Fdiscslab";
			{
				ArrayList<Discslab> list =new ArrayList<Discslab>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FdiscslabResult");
				Log.v(TAG, "Array Length Discslab DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscslabDS ds =new DiscslabDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discslab discslab =new Discslab();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discslab.setFDISCSLAB_REF_NO(jObject.getString("RefNo"));
					discslab.setFDISCSLAB_SEQ_NO(jObject.getString("seqno"));
					discslab.setFDISCSLAB_QTY_F(jObject.getString("Qtyf"));
					discslab.setFDISCSLAB_QTY_T(jObject.getString("Qtyt"));
					discslab.setFDISCSLAB_DIS_PER(jObject.getString("disper"));
					discslab.setFDISCSLAB_DIS_AMUT(jObject.getString("disamt"));
					discslab.setFDISCSLAB_TIMESTAMP_COLUMN(jObject.getString("timestamp_column"));
															
					list.add(discslab);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDiscslab(list)>0){
						Log.v("createOrUpdateDiscslab", "Result : Discslab Data Inserted successfully");
					}
					
				}
			}	
			break;
				
			case FDISCHED:
			//downloadingDataType ="FDisched";
			{
				ArrayList<Disched> list =new ArrayList<Disched>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FDischedResult");
				Log.v(TAG, "Array Length Disched DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DischedDS ds =new DischedDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Disched disched =new Disched();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					disched.setFDISCHED_REF_NO(jObject.getString("RefNo"));
					disched.setFDISCHED_TXN_DATE(jObject.getString("TxnDate"));
					disched.setFDISCHED_DISC_DESC(jObject.getString("DiscDesc"));
					disched.setFDISCHED_PRIORITY(jObject.getString("Priority"));
					disched.setFDISCHED_DIS_TYPE(jObject.getString("DisType"));
					disched.setFDISCHED_V_DATE_F(jObject.getString("Vdatef"));
					disched.setFDISCHED_V_DATE_T(jObject.getString("Vdatet"));
					disched.setFDISCHED_REMARK(jObject.getString("Remarks"));
					disched.setFDISCHED_ADD_USER(jObject.getString("AddUser"));
					disched.setFDISCHED_ADD_DATE(jObject.getString("AddDate"));
					disched.setFDISCHED_ADD_MACH(jObject.getString("AddMach"));
					disched.setFDISCHED_RECORD_ID(jObject.getString("RecordId"));
					disched.setFDISCHED_TIMESTAMP_COLUMN(jObject.getString("timestamp_column"));
				
					
					list.add(disched);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDisched(list)>0){
						Log.v("createOrUpdateDisched", "Result : Disched Data Inserted successfully");
					}
					
				}
			}
			break;
				
			case FFREEITEM:
			{
				ArrayList<FreeItem> list =new ArrayList<FreeItem>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fFreeItemResult");
				Log.v(TAG, "Array Length FreeItem DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				FreeItemDS ds =new FreeItemDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					FreeItem freeItem =new FreeItem();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					freeItem.setFFREEITEM_ITEMCODE(jObject.getString("Itemcode"));
					freeItem.setFFREEITEM_REFNO(jObject.getString("Refno"));

					list.add(freeItem);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFreeItem(list)>0){
						Log.v("createOrUpdateFreeItem", "Result : FreeItem Data Inserted successfully");
					}
					
				}
			}	
			break;
			case FFREEMSLAB:
			//downloadingDataType="FfreeMslab";
			{
				ArrayList<FreeMslab> list =new ArrayList<FreeMslab>();
				JSONArray jsonArray = jsonResponse.getJSONArray("fFreeMslabResult");
				Log.v(TAG, "Array Length FreeMslab DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				FreeMslabDS ds =new FreeMslabDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					FreeMslab freeMslab =new FreeMslab();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					freeMslab.setFFREEMSLAB_REFNO(jObject.getString("Refno"));
					freeMslab.setFFREEMSLAB_QTY_F(jObject.getString("Qtyf"));
					freeMslab.setFFREEMSLAB_QTY_T(jObject.getString("Qtyt"));
					freeMslab.setFFREEMSLAB_ITEM_QTY(jObject.getString("ItemQty"));
					freeMslab.setFFREEMSLAB_FREE_IT_QTY(jObject.getString("FreeItQty"));
					freeMslab.setFFREEMSLAB_ADD_USER(jObject.getString("AddUser"));
					freeMslab.setFFREEMSLAB_ADD_DATE(jObject.getString("AddDate"));
					freeMslab.setFFREEMSLAB_ADD_MACH(jObject.getString("AddMach"));
					freeMslab.setFFREEMSLAB_SEQ_NO(jObject.getString("Seqno"));
															
					list.add(freeMslab);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFreeMslab(list)>0){
						Log.v("createOrUpdatefreeMslab", "Result : freeMslab Data Inserted successfully");
					}
					
				}
			}	
			break;	
			case FDISCVHED:
			//downloadingDataType="FDiscvhed";
			{
				ArrayList<Discvhed> list =new ArrayList<Discvhed>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FDiscvhedResult");
				Log.v(TAG, "Array Length Discvhed DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscvhedDS ds =new DiscvhedDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discvhed discvhed =new Discvhed();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discvhed.setFDISCVHED_REF_NO(jObject.getString("Refno"));
					discvhed.setFDISCVHED_TXN_DATE(jObject.getString("Txndate"));
					discvhed.setFDISCVHED_DISC_DESC(jObject.getString("DiscDesc"));
					discvhed.setFDISCVHED_PRIORITY(jObject.getString("Priority"));
					discvhed.setFDISCVHED_DIS_TYPE(jObject.getString("DisType"));
					discvhed.setFDISCVHED_V_DATE_F(jObject.getString("Vdatef"));
					discvhed.setFDISCVHED_V_DATE_T(jObject.getString("Vdatet"));
					discvhed.setFDISCVHED_REMARKS(jObject.getString("Remarks"));
															
					list.add(discvhed);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDiscvhed(list)>0){
						Log.v("createOrUpdateDiscvhed", "Result : Discvhed Data Inserted successfully");
					}
					
				}
			}
			break;
			case FDISCVDET:
			//downloadingDataType="FDiscvdet";
			{
				ArrayList<Discvdet> list =new ArrayList<Discvdet>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FdiscvdetResult");
				Log.v(TAG, "Array Length Discvhed DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscvdetDS ds =new DiscvdetDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discvdet discvdet =new Discvdet();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discvdet.setFDISCVDET_REF_NO(jObject.getString("Refno"));
					discvdet.setFDISCVDET_VALUE_F(jObject.getString("Valuef"));
					discvdet.setFDISCVDET_VALUE_T(jObject.getString("Valuet"));
					discvdet.setFDISCVDET_DISPER(jObject.getString("Disper"));
					discvdet.setFDISCVDET_DIS_AMT(jObject.getString("Disamt"));
															
					list.add(discvdet);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDiscvdet(list)>0){
						Log.v("createOrUpdateDiscvdet", "Result : Discvdet Data Inserted successfully");
					}
					
				}
			}
			break;
			case FDISCVDEB:
			//downloadingDataType="FDiscvdeb";
			{
				ArrayList<Discvdeb> list =new ArrayList<Discvdeb>();
				JSONArray jsonArray = jsonResponse.getJSONArray("FDiscvdebResult");
				Log.v(TAG, "Array Length Discvdeb DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				DiscvdebDS ds =new DiscvdebDS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					Discvdeb discvdeb =new Discvdeb();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					discvdeb.setFDISCVDEB_REF_NO(jObject.getString("Refno"));
					discvdeb.setFDISCVDED_DEB_CODE(jObject.getString("Debcode"));
																				
					list.add(discvdeb);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateDiscvdeb(list)>0){
						Log.v("createOrUpdateDiscvdeb", "Result : Discvdeb Data Inserted successfully");
					}
					
				}
			}	
			break;
			case FINVHEDL3:
			//downloadingDataType="FinvHedL3";
			{
				ArrayList<FinvHedL3> list =new ArrayList<FinvHedL3>();
				JSONArray jsonArray = jsonResponse.getJSONArray("RepLastThreeInvHedResult");
				Log.v(TAG, "Array Length FinvHedL3 DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				FinvHedL3DS ds =new FinvHedL3DS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					FinvHedL3 finvHedL3 =new FinvHedL3();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					finvHedL3.setFINVHEDL3_DEB_CODE(jObject.getString("DebCode"));
					finvHedL3.setFINVHEDL3_REF_NO(jObject.getString("RefNo"));
					finvHedL3.setFINVHEDL3_REF_NO1(jObject.getString("RefNo1"));
					finvHedL3.setFINVHEDL3_TOTAL_AMT(jObject.getString("TotalAmt"));
					finvHedL3.setFINVHEDL3_TOTAL_TAX(jObject.getString("TotalTax"));
					finvHedL3.setFINVHEDL3_TXN_DATE(jObject.getString("TxnDate"));
																								
					list.add(finvHedL3);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFinvHedL3(list)>0){
						Log.v("createOrUpdatefinvHedL3", "Result : finvHedL3 Data Inserted successfully");
					}
					
				}
			}
			break;
			case FINVDETL3:
			//downloadingDataType="FinvDetL3";
			{
				ArrayList<FinvDetL3> list =new ArrayList<FinvDetL3>();
				JSONArray jsonArray = jsonResponse.getJSONArray("RepLastThreeInvDetResult");
				Log.v(TAG, "Array Length FinvDetL3 DB :" + jsonArray.length());
				
				totalRecords=jsonArray.length();
								
				FinvDetL3DS ds =new FinvDetL3DS(context);
				Log.v(TAG,"Deleted Count: "+ds.deleteAll());
				
				for (int i = 0; i < jsonArray.length(); i++)
				{
					FinvDetL3 finvDetL3 =new FinvDetL3();
				
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					list.clear();
					
					finvDetL3.setFINVDETL3_AMT(jObject.getString("Amt"));
					finvDetL3.setFINVDETL3_ITEM_CODE(jObject.getString("ItemCode"));
					finvDetL3.setFINVDETL3_QTY(jObject.getString("Qty"));
					finvDetL3.setFINVDETL3_REF_NO(jObject.getString("RefNo"));
					finvDetL3.setFINVDETL3_SEQ_NO(jObject.getString("SeqNo"));
					finvDetL3.setFINVDETL3_TAX_AMT(jObject.getString("TaxAmt"));
					finvDetL3.setFINVDETL3_TAX_COM_CODE(jObject.getString("TaxComCode"));
					finvDetL3.setFINVDETL3_TXN_DATE(jObject.getString("TxnDate"));
									
					list.add(finvDetL3);
					
					++recordCount;
					publishProgress(recordCount);
					
					if(ds.createOrUpdateFinvDetL3(list)>0){
						Log.v("createOrUpdatefinvDetL3", "Result : finvDetL3 Data Inserted successfully");
					}
					
				}
			}
			break;
			default:
				break;
			}
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "FileNotFound";
		}catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// return JSON String
		return resultStr;
	}
	
	@Override
    protected void onProgressUpdate(Integer... progress) {
    	super.onProgressUpdate(progress);
    	String titleMsg = "Downloading " + downloadingDataType + " data";
    	progressDialog.setMessage(titleMsg+" " + progress[0] + "/" + totalRecords);
    	 
    }
    
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		
		progressDialog.dismiss();
		taskListener.onTaskCompleted(taskType, result);

	}

}
